package com.zego.wawaji.ui.coupon.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.base.smarttab.PagerItem;
import com.zego.wawaji.ui.base.smarttab.SmartTabLayout;
import com.zego.wawaji.ui.base.viewpager.ViewGroupPagerAdapter;
import com.zego.wawaji.ui.coupon.bean.Coupon;
import com.zego.wawaji.ui.coupon.bean.CouponList;
import com.zego.wawaji.ui.coupon.panel.enable.CouponEnablePanel;
import com.zego.wawaji.ui.coupon.panel.invalid.CouponInvalidPanel;
import com.zego.wawaji.ui.coupon.panel.used.CouponUsedPanel;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的优惠券
 * Created by Su on 2018/2/8.
 */
public class CouponActivity extends BaseAppCompatActivity implements PObserver, RequestComplete {
    private SmartTabLayout stlTitles;
    private ViewPager vpViewChange;
    private List<PagerItem> listViews;//pagerItem集合
    private List<BasePanel> panls = new ArrayList<>(); // Tab页面列表

    private CouponEnablePanel enablePanel;
    private CouponUsedPanel usedPanel;
    private CouponInvalidPanel invalidPanel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coupon_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("优惠券");

        MsgMgr.getInstance().attach(this);
        stlTitles = (SmartTabLayout) findViewById(R.id.stl_titles);
        vpViewChange = (ViewPager) findViewById(R.id.viewpager);
        ModuleMgr.getWawaMgr().reqUserCoupon(this);
    }

    /**
     * 添加展示panel
     */
    private void initViewsList(CouponList list) {
        listViews = new ArrayList<>();
        List<Coupon> enableList = list.getCouponEnableList();
        List<Coupon> usedList = list.getCouponUsedList();
        List<Coupon> invalidList = list.getCouponInvalidList();

        enablePanel = new CouponEnablePanel(this, enableList);
        usedPanel = new CouponUsedPanel(this, usedList);
        invalidPanel = new CouponInvalidPanel(this, invalidList);

        panls.add(enablePanel);
        panls.add(usedPanel);
        panls.add(invalidPanel);
        listViews.add(new PagerItem("可使用(" + enableList.size() + ")", panls.get(0).getContentView()));
        listViews.add(new PagerItem("已使用(" + usedList.size() + ")", panls.get(1).getContentView()));
        listViews.add(new PagerItem("已失效(" + invalidList.size() + ")", panls.get(2).getContentView()));

        vpViewChange.setAdapter(new ViewGroupPagerAdapter(listViews));
        ((LinearLayout) stlTitles.getTabStrip()).setGravity(Gravity.CENTER_HORIZONTAL);//标题居中
        stlTitles.setCustomTabView(R.layout.custom_table_view, R.id.tv_left_tab);//设置自定义标题
        stlTitles.setViewPager(vpViewChange);//设置viewpager
        stlTitles.setOnPageChangeListener(null);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:   // 充值成功更新个人资料
                refresh();
                break;
        }
    }

    private void refresh() {
        PickerDialogUtil.closePayDialog();
        ModuleMgr.getWawaMgr().reqUserCoupon(response -> {
            if (!response.isOk()) {
                return;
            }
            CouponList couponList = (CouponList) response.getBaseData();
            if (couponList == null) return;

            if (enablePanel != null) {
                enablePanel.refresh(couponList.getCouponEnableList());
            }
            if (usedPanel != null) {
                usedPanel.refresh(couponList.getCouponUsedList());
            }
            if (invalidPanel != null) {
                invalidPanel.refresh(couponList.getCouponInvalidList());
            }
        });
    }

    @Override
    public void onRequestComplete(final HttpResponse response) {
        CouponList list;
        if (!response.isOk()) {
            list = new CouponList();
        } else {
            list = (CouponList) response.getBaseData();
        }
        initViewsList(list);
    }
}
